package edu.szcu.vitaguard.service.impl;

import edu.szcu.vitaguard.dao.DoctorDao;
import edu.szcu.vitaguard.entity.Doctor;
import edu.szcu.vitaguard.service.DoctorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 医生服务实现类
 */
@Service
public class DoctorServiceImpl implements DoctorService {

    @Autowired
    private DoctorDao doctorDao;

    @Override
    @Transactional
    public Doctor addDoctor(Doctor doctor) {
        // 设置创建时间和更新时间
        LocalDateTime now = LocalDateTime.now();
        doctor.setCreateTime(now);
        doctor.setUpdateTime(now);

        // 如果状态为空，设置默认状态为"正常"
        if (doctor.getStatus() == null || doctor.getStatus().isEmpty()) {
            doctor.setStatus("正常");
        }

        // 插入医生记录
        doctorDao.insert(doctor);
        return doctor;
    }

    @Override
    public Doctor getDoctorById(Integer doctorId) {
        return doctorDao.selectById(doctorId);
    }

    @Override
    public List<Doctor> getAllDoctors() {
        return doctorDao.selectAll();
    }

    @Override
    public List<Doctor> getDoctorsByDeptId(Integer deptId) {
        return doctorDao.selectByDeptId(deptId);
    }

    @Override
    public List<Doctor> getDoctorsByName(String doctorName) {
        return doctorDao.selectByName(doctorName);
    }

    @Override
    public List<Doctor> getDoctorsByStatus(String status) {
        return doctorDao.selectByStatus(status);
    }

    @Override
    @Transactional
    public Doctor updateDoctor(Doctor doctor) {
        // 设置更新时间
        doctor.setUpdateTime(LocalDateTime.now());

        // 更新医生记录
        doctorDao.update(doctor);
        return doctorDao.selectById(doctor.getDoctorId());
    }

    @Override
    @Transactional
    public boolean deleteDoctor(Integer doctorId) {
        return doctorDao.deleteById(doctorId) > 0;
    }
}
